from typing import Optional, List
from datetime import datetime
from pydantic import BaseModel, Field

class RoleBase(BaseModel):
    """角色基础Schema"""
    name: str = Field(..., min_length=2, max_length=50)
    display_name: str = Field(..., min_length=2, max_length=100)
    description: Optional[str] = None
    level: int = Field(1, ge=1, le=10)
    is_active: bool = True
    is_system: bool = False


class RoleCreate(RoleBase):
    """角色创建Schema"""
    permission_ids: Optional[List[int]] = []


class RoleUpdate(BaseModel):
    """角色更新Schema"""
    name: Optional[str] = Field(None, min_length=2, max_length=50)
    display_name: Optional[str] = Field(None, min_length=2, max_length=100)
    description: Optional[str] = None
    level: Optional[int] = Field(None, ge=1, le=10)
    is_active: Optional[bool] = None
    permission_ids: Optional[List[int]] = None


class RoleInDBBase(RoleBase):
    """数据库中的角色基础信息"""
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class Role(RoleInDBBase):
    """角色返回Schema"""
    permissions: List = []  # 避免前向引用
    user_count: int = 0


class RoleInDB(RoleInDBBase):
    """数据库中的完整角色信息"""
    pass


class RoleWithPermissions(Role):
    """带权限的角色信息"""
    permissions: List = []  # 避免前向引用
    permission_names: List[str] = []


class RoleList(BaseModel):
    """角色列表Schema"""
    items: List[Role]
    total: int
    page: int
    size: int